﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entities;
using System.Web.Security;

namespace WebApplication.Admins.Controls
{
    public partial class UCEditTinTuc : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Display();
            }

        }
        public void Display()
        {
            dllnguoidung.DataValueField = "UserId";
            dllnguoidung.DataTextField = "UserName";
            dllnguoidung.DataSource = User.LayTatCa();
            dllnguoidung.DataBind();

            string cid = Request.QueryString["Id"] ?? "";
            if (cid != "")
            {

                Entities.TinTuc data = TinTuc_BLL.LayID(cid);
                if (data == null)
                    Response.Redirect("~/Admins/TinTuc.aspx");
                lblTitle.Text = "Quản Lý Tin Tức";
                lblId.Text = data.IDTinTuc.ToString();
                txtTieuDe.Text = data.TieuDe;
               // string noidung = txtNoiDung.Value;
                txtMoTaEdit.InnerHtml = data.MoTa;
                txtMoTa.Value = txtMoTaEdit.InnerText;
                txtNoiDungEdit.InnerHtml = data.NoiDung;
                txtNoiDung.Value = txtNoiDungEdit.InnerText;
              // noidung = data.NoiDung;
                txtHinhAnh.Text = data.HinhAnh;
                if (data.HienThi == true)
                    rdTrue.Checked = true;
                else
                    rdFales.Checked = true;
                txtNgayDang.Text = data.NgayDang.ToString("dd/MM/yyyy");
                dllnguoidung.SelectedValue =data.UserID.ToString();
                btnReset.Visible = true;
            }
            else
            {
                lblTitle.Text = "Thêm Tin Tức";
                btnReset.Visible = true;
            
            }
        }
        public Entities.TinTuc getData()
        {
            Entities.TinTuc data = null;
            if (lblId.Text != "")
                data = BLL.TinTuc_BLL.LayID(lblId.Text);
            else
                data = new Entities.TinTuc();
            data.TieuDe = txtTieuDe.Text;
            //string noidung = txtNoiDung.Value;
            data.MoTa = HttpUtility.HtmlAttributeEncode(txtMoTa.Value);
            data.NoiDung = HttpUtility.HtmlAttributeEncode(txtNoiDung.Value);
            data.HinhAnh = txtHinhAnh.Text;
            if (rdTrue.Checked == true)
                data.HienThi = true;
            else
                data.HienThi = false;
            data.NgayDang = DateTime.ParseExact(txtNgayDang.Text, "dd/MM/yyyy", null);
            data.UserID = Guid.Parse(dllnguoidung.SelectedValue);
            return data;
        }
        private void Reset()
        {
            txtTieuDe.Text = "";
            txtNoiDung.Value =null;
            txtHinhAnh.Text = "";
            txtNgayDang.Text = DateTime.Now.ToString("dd/MM/yyyy");
            dllnguoidung.SelectedValue = "0";
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                bool rs = false;
                Entities.TinTuc data = getData();
                if (data.IDTinTuc > 0)
                {
                    rs = BLL.TinTuc_BLL.Sua(data);
                    lblStatus.Text = rs ? "Update thành công" : "Update thất bại";
                }
                else
                {
                    rs = BLL.TinTuc_BLL.Them(data);
                    lblStatus.Text = rs ? "Insert thành công" : "Insert thất bại";
                    if (rs)
                        Reset();
                }
            }


        }


    }
}